import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * User: yuanjianpeng
 * Date: 2018/10/17
 * Project: httpclient
 * Version: 1.0
 * Description: 模拟本地网络服务
 */
public class ServerMock extends Thread {

    private ServerSocket serverSocket;

    public ServerMock(int port) throws IOException {
        serverSocket = new ServerSocket(port);
    }

    @Override
    public void run() {
        while(true){
            Socket serverClient = null;
            try {
                serverClient = serverSocket.accept();

                System.out.println(System.currentTimeMillis()+"欢迎连接"+serverClient.getRemoteSocketAddress());

                //读取客户端发送来的消息
                BufferedReader br = new BufferedReader(new InputStreamReader(serverClient.getInputStream()));
                //DataInputStream
                String mess = br.readLine();
                System.out.println(mess);

                //向客户端返回信息
                BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(serverClient.getOutputStream()));
                bw.write("response\n");
                bw.flush();

                Thread.sleep(5000);
                System.out.println(System.currentTimeMillis()+"谢谢连接"+serverClient.getRemoteSocketAddress());

                serverClient.close();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        try {
            Thread t = new ServerMock(6066);
            t.run();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
